package com.robinmouren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.robinmouren.constant.MessageConstant;
import com.robinmouren.constant.StatusConstant;
import com.robinmouren.entity.OrderInfo;
import com.robinmouren.entity.Quote;
import com.robinmouren.exception.NullOrderInfoException;
import com.robinmouren.mapper.QuoteMapper;
import com.robinmouren.service.IQuoteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单报价表 服务实现类
 * </p>
 *
 * @author Robinmouren
 * @since 2024-05-12
 */
@Service
public class QuoteServiceImpl extends ServiceImpl<QuoteMapper, Quote> implements IQuoteService {


    @Autowired
    private OrderInfoServiceImpl orderInfoService;

    /**
     * 根据订单号获取报价
     * @param orderNumber
     * @return
     */
    @Override
    public Quote quoteGetById(String orderNumber) {

        OrderInfo orderinfo = orderInfoService.getOne(new LambdaQueryWrapper<OrderInfo>()
                .eq(OrderInfo::getOrderNumber, orderNumber)
        );
        if(orderinfo == null){
            throw new NullOrderInfoException(MessageConstant.ORDER_IS_NULL);
        }
        Quote one = this.getOne(new LambdaQueryWrapper<Quote>()
                .eq(Quote::getOrderId, orderinfo.getId())
                .and(wrapper -> {
                    wrapper.eq(Quote::getStatus, StatusConstant.QUOTED_CARRIERS);
                }));
        if(one == null){
            throw new NullOrderInfoException(MessageConstant.QUOTE_IS_NULL);
        }
        return one;
    }
}
